package com.j2data.spike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2data.spike.mapper.SpikeOrderMapper;
import com.j2data.spike.pojo.SpikeOrder;
import com.j2data.spike.pojo.User;
import com.j2data.spike.service.ISpikeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author j2data
 * @since 2022-07-05
 */
@Service
public class SpikeOrderServiceImpl extends ServiceImpl<SpikeOrderMapper, SpikeOrder> implements ISpikeOrderService {

    @Autowired
    private SpikeOrderMapper spikeOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param user
     * @param goodsId
     * @return
     */
    @Override
    public Long getResult(User user, Long goodsId) {
        SpikeOrder spikeOrder = spikeOrderMapper.selectOne(new QueryWrapper<SpikeOrder>()
                .eq("user_id", user.getId())
                .eq("goods_id", goodsId));
        if (spikeOrder != null) {
            return spikeOrder.getId();
        } else if (redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
            return -1L;
        } else {
            return 0L;
        }
    }
}
